/*
 * Copyright (c) 2020 pig4cloud Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.taiji.dicom4cloud.common.datasource.config;

import com.baomidou.dynamic.datasource.provider.AbstractJdbcDataSourceProvider;
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty;
import com.taiji.dicom4cloud.common.core.constrant.CommonConstants;
import com.taiji.dicom4cloud.common.datasource.contrant.DataSourceConstants;
import com.taiji.dicom4cloud.common.datasource.property.DicomDataSourceProperties;
import lombok.extern.slf4j.Slf4j;
import org.jasypt.encryption.StringEncryptor;

import javax.sql.DataSource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;

/**
 * @author fanxl
 * @date 2020/2/6
 * <p>
 * 从数据源中获取 配置信息
 */
@Slf4j
public class JdbcDynamicDataSourceProvider extends AbstractJdbcDataSourceProvider {

	private final DicomDataSourceProperties properties;

	private final StringEncryptor stringEncryptor;

	private Map<String,DataSourceProperty> allDataSourceProperty;

	public JdbcDynamicDataSourceProvider(StringEncryptor stringEncryptor, DicomDataSourceProperties properties) {
		super(properties.getDriverClassName(), properties.getUrl(), properties.getUsername(), properties.getPassword());
		this.stringEncryptor = stringEncryptor;
		this.properties = properties;
		allDataSourceProperty = new HashMap<>(16);
	}
	/**
	 * 执行语句获得数据源参数
	 * @param statement 语句
	 * @return 数据源参数
	 * @throws SQLException sql异常
	 */
	@Override
	protected Map<String, DataSourceProperty> executeStmt(Statement statement) throws SQLException {

		Map<String, DataSourceProperty> map = new HashMap<>(8);
		if(properties.getIsDynamic()){
			ResultSet rs;
			if(properties.getIsDynamic()){
				rs = statement.executeQuery(properties.getQueryDsSql()+ CommonConstants.STATUS_NORMAL);
			}else{
				rs = statement.executeQuery(properties.getQueryDsSql()+ CommonConstants.STATUS_DEL);
			}
			while (rs.next()) {
				String name = rs.getString(DataSourceConstants.DS_NAME);
				String username = rs.getString(DataSourceConstants.DS_USER_NAME);
				String password = rs.getString(DataSourceConstants.DS_USER_PWD);
				String driver = rs.getString(DataSourceConstants.DS_DRIVER);
				String url = rs.getString(DataSourceConstants.DS_JDBC_URL);
				DataSourceProperty property = new DataSourceProperty();
				property.setDriverClassName(driver);
				property.setUsername(username);
				property.setPassword(password);
				property.setUrl(url);
				map.put(name, property);
				allDataSourceProperty.put(name,property);
				log.info("加载从数据源,url={}",url);
			}
		}
		// 添加默认主数据源
		DataSourceProperty property = new DataSourceProperty();
		property.setUsername(properties.getUsername());
		property.setPassword(properties.getPassword());
		property.setUrl(properties.getUrl());
		property.setDriverClassName(properties.getDriverClassName());
		map.put(DataSourceConstants.DS_MASTER, property);
		allDataSourceProperty.put(DataSourceConstants.DS_MASTER,property);
		log.info("加载主数据源,url={}",properties.getUrl());
		return map;
	}

	public DataSourceProperty getDataSourceProperty(String name){
		return allDataSourceProperty.get(name);
	}
}
